"""
ETF数据API路由
"""
import logging
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, Query, Path
from sqlalchemy.orm import Session

from modules.models.base import get_db
from modules.models.stock import SecurityInfo
from modules.api.auth import get_current_active_user, User

# 配置日志
logger = logging.getLogger("api.etfs")

# 创建路由
router = APIRouter()


@router.get("/list", summary="获取ETF列表")
async def get_etf_list(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取ETF列表
    """
    try:
        etfs = db.query(SecurityInfo).filter(
            SecurityInfo.type == 'etf',
            SecurityInfo.is_active == 1
        ).all()
        
        return etfs
    
    except Exception as e:
        logger.error(f"获取ETF列表失败: {str(e)}")
        raise HTTPException(status_code=500, detail=f"获取ETF列表失败: {str(e)}")


@router.get("/{code}", summary="获取单个ETF信息")
async def get_etf_info(
    code: str = Path(..., description="ETF代码"),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取单个ETF信息
    
    - **code**: ETF代码
    """
    try:
        etf = db.query(SecurityInfo).filter(
            SecurityInfo.code == code,
            SecurityInfo.type == 'etf'
        ).first()
        
        if not etf:
            raise HTTPException(status_code=404, detail=f"ETF代码 {code} 不存在")
        
        return etf
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"获取ETF {code} 信息失败: {str(e)}")
        raise HTTPException(status_code=500, detail=f"获取ETF信息失败: {str(e)}")


@router.get("/{code}/quotes", summary="获取ETF行情数据")
async def get_etf_quotes(
    code: str = Path(..., description="ETF代码"),
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    获取ETF行情数据（骨架API，待完善）
    
    - **code**: ETF代码
    """
    return {"message": "该功能正在开发中"}